<template>
  <div class="app-container">
    <!--form 组件-->
    <eForm ref="form" :is-add="isAdd"/>
    <div class="container">
      <el-row :gutter="20">
        <!--部门数据-->
        <el-col :xs="7" :sm="6" :md="4" :lg="4" :xl="4">
          <el-tree :data="varietys" :props="defaultProps" :expand-on-click-node="false" node-key="variety_id" @node-click="handleNodeClick"/>
        </el-col>
        <!--用户数据-->
        <el-col :xs="17" :sm="18" :md="20" :lg="20" :xl="20">
          <!--工具栏-->
          <div>
            <!-- 搜索 -->
            <el-form ref="query" :inline="true" :model="query" size="small" class="demo-form-inline">
              <el-form-item label="参数名称：" prop="parameter_name">
                <el-input v-model="query.parameter_name" clearable placeholder="输入参数名称搜索" style="width: 180px;" class="filter-item" @keyup.enter.native="toQuery"/>
              </el-form-item>
              <el-form-item>
                <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
                <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                <!-- 新增 -->
                <div style="display: inline-block;margin: 0px 2px;">
                  <el-button
                    class="filter-item"
                    size="mini"
                    type="primary"
                    icon="el-icon-plus"
                    @click="add">新增</el-button>
                </div>
              </el-form-item>
            </el-form>
          </div>
          <!--表格渲染-->
          <el-table v-loading="loading" :data="data" height="450" size="small" style="width: 100%;">
            <el-table-column prop="parameter_name" label="参数"/>
            <el-table-column prop="parameter_sort" label="排序"/>
            <el-table-column prop="is_required" label="是否必填">
              <template slot-scope="scope">
                <span>{{ displayRequired(scope.row.is_required) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="125" align="center">
              <template slot-scope="scope">
                <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
                <el-popover
                  :ref="scope.row.parameter_id"
                  placement="top"
                  width="180">
                  <p>确定删除本条数据吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="small" @click="$refs[scope.row.parameter_id].doClose()">取消</el-button>
                    <el-button :loading="delLoading" type="primary" size="small" @click="subDelete(scope.row.parameter_id)">确定</el-button>
                  </div>
                  <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
                </el-popover>
              </template>
            </el-table-column>
          </el-table>
          <!--分页组件-->
          <el-pagination
            :total="total"
            :current-page="page + 1"
            background
            style="margin-top: 8px;"
            layout="total, prev, pager, next, sizes"
            @size-change="sizeChange"
            @current-change="pageChange"/>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import { getVarietys, del } from '@/api/mall/parameter'
import eForm from './form'
export default {
  components: { eForm },
  mixins: [initData],
  data() {
    return {
      isAdd: false,
      delLoading: false,
      varietys: [],
      variety_id: null,
      path_name: '',
      query: {
        parameter_name: ''
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created() {
    this.getVarietyDatas()
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    beforeInit() {
      this.url = 'api/product-parameter-list'
      const sort = 'parameter_sort,asc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      const parameter_name = query.parameter_name
      if (parameter_name) {
        this.params['parameter_name'] = parameter_name
      }
      if (this.variety_id) {
        this.params['variety_id'] = this.variety_id
      }
      return true
    },
    displayRequired(key) {
      return key ? '是' : '否'
    },
    subDelete(parameter_id) {
      this.delLoading = true
      del(parameter_id).then(res => {
        if (res.result) {
          this.delLoading = false
          this.$refs[parameter_id].doClose()
          this.dleChangePage()
          this.init()
          this.$notify({
            title: res.msg,
            type: 'success',
            duration: 2500
          })
        } else {
          this.$notify({
            title: res.msg,
            type: 'error',
            duration: 2500
          })
        }
      }).catch(err => {
        this.delLoading = false
        this.$refs[parameter_id].doClose()
        console.log(err.response.data.message)
      })
    },
    getVarietyDatas() {
      const sort = 'create_time,asc'
      const param = { sort: sort }
      getVarietys(param).then(res => {
        const list = res.content
        this.varietys = list
        this.init()
      })
    },
    handleNodeClick(data) {
      if (data.variety_id) {
        this.variety_id = data.variety_id
      } else {
        this.variety_id = null
      }
      if (data.path_name) {
        this.path_name = data.path_name
      } else {
        this.path_name = ''
      }
      this.init()
    },
    add() {
      if (this.variety_id == null) {
        this.$message({
          message: '请先选择左侧分类后再新增',
          type: 'warning'
        })
        return false
      }
      this.isAdd = true
      const _this = this.$refs.form
      _this.form = { path_name: this.path_name, variety_id: this.variety_id, is_required: 'false' }
      _this.dialog = true
    },
    edit(row) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = { path_name: row.path_name, parameter_id: row.parameter_id, parameter_name: row.parameter_name, parameter_sort: row.parameter_sort, is_required: row.is_required.toString(), variety_id: row.variety_id }
      _this.dialog = true
    },
    resetQuery() {
      this.$refs['query'].resetFields()
      this.variety_id = null
      this.path_name = ''
      this.init()
    }
  }
}
</script>

<style scoped>
</style>
